<?php


namespace app\admin\controller;


use app\admin\model\CustomerStatusModel;
use app\admin\model\IntentionTagModel;
use app\admin\model\LostReasonModel;
use app\admin\model\RoleUserModel;
use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;

class LostReasonController extends AdminBaseController
{
    public function index()
    {
        $admin_id = cmf_get_current_admin_id();
        $role_id = RoleUserModel::where('user_id', $admin_id)->value('role_id');
        $role_id = $role_id ? $role_id : 1;
        $this->assign('role_id', $role_id);
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $admin_id = cmf_get_current_admin_id();
            $user_info = UserModel::get($admin_id);
            $where = [];
            if (isset($data['company_id']) && $data['company_id']) {
                $where['company_id'] = $data['company_id'];
            } else {
                if ($user_info['company_id']) {
                    $where['company_id'] = $user_info['company_id'];
                }
            }

            $list = LostReasonModel::with(['company_info'])
                ->where($where)
                ->order('sort asc')
                ->all()->each(function ($item) {
                    if ($item['options']) {
                        $item['options'] = json_decode($item['options']);
                    }
                    return $item;
                });
            $this->success('', '', $list);
        }
        return $this->fetch();
    }

    public function update()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $admin_id = cmf_get_current_admin_id();
            $user_info = UserModel::get($admin_id);
            if ($user_info['company_id']) {
                $data['company_id'] = $user_info['company_id'];
            }
            $data['options'] = json_encode($data['options']);
            if (isset($data['id']) && $data['id']) {
                $model = LostReasonModel::get($data['id']);
                $model->name = $data['name'];
                $model->options = $data['options'];
                if ($data['company_id']) {
                    $model->company_id = $data['company_id'];
                }
            } else {
                $model = LostReasonModel::create($data);
            }
            $res = $model->save();
            if ($res) {

                $this->success('ok');
            } else {
                $this->error('fail');
            }
        }
    }

    public function change_status()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $setting = LostReasonModel::get($data['id']);
            if ($setting) {
                $res = LostReasonModel::where('id', $data['id'])->update([$data['field'] => $data['value']]);
                if ($res) {
                    $this->success('ok');
                } else {
                    $this->error('error');
                }
            } else {
                $this->error('数据异常');
            }
        }
    }

    public function delete()
    {
        $model = LostReasonModel::get(input('post.id'));
        if ($model) {
            $res = $model->delete();
            if ($res) {
                $this->success('ok');
            } else {
                $this->error('fail');
            }
        } else {
            $this->error('数据异常');
        }
    }
}